import * as cmd from './cmd';
import { deviceStore } from '~/store';
import BleModule from './bleModule';
const BluetoothManager = new BleModule();

const createBuffer = (dataArray) => {
    let buffer = new ArrayBuffer(dataArray.length);
    let dataView = new DataView(buffer);
    dataArray.forEach((value, index) => dataView.setUint8(index, value));
    return dataView;
};

const getDeviceId = (remote = { type: 'ble' }) => {
    return remote.type == 'remote' ? '' : deviceStore.device.connectDevice.id;
};

// 查询设备电量
export const getBleEQ = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GetEQ, createBuffer([]), '查询设备电量', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 配网
export const setWIFINetWork = (sendArray,) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_setWIFI, createBuffer(sendArray), '设备蓝牙配网', getDeviceId())
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

export const setFactoryHideBloodHeartStatus = (isOpen) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_SETHEARTBLOOD, createBuffer([isOpen]), '设置新法规血氧心率开关', getDeviceId(),)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 查询设置的手动激光参数
export const getLaserParams = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GetLaserManuallyParameters, createBuffer([]), '查询设置的激光参数', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 设置手动激光参数
export const setLaserParams = (power, duration, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_LaserManuallyParameters, createBuffer([power, duration]), '设置手动激光参数', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 开启手动激光
export const openLaser = (status, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_LaserIsOpen, createBuffer([status]), '开启手动激光', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};



// 开启手动心率
export const openHeart = (status, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_HRManuallyIsOpen, createBuffer([status]), '开启手动心率', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 开启关闭实时步数心率值检测
export const isOpenRealtimeData = (isOpen, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_RealtimeIsOpen, createBuffer([isOpen]), '开启关闭实时步数心率值检测', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 开关实时血压
export const isOpenManullyBlood = (isOpen, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_TEREALBLOODISOPEN, createBuffer([isOpen, isOpen]), '开关实时血压', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 实时血压值
export const openBloodData = (isOpen, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_TEREALBLOODISDATA, createBuffer([isOpen]), '实时血压值', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 查询血压状态开关状态
export const GetBoldStatus = (isOpen, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_BOLD_STATUS, createBuffer([isOpen]), '查询血压状态开关状态', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 抬手亮屏状态设置
export const setScreenLight = (isOpen, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_SETSCREENLIGHT, createBuffer([isOpen]), '抬手亮屏状态设置', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 查询抬手亮屏状态设置
export const getScreenLight = (isOpen, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GETSCREENLIGHT, createBuffer([isOpen]), '查询抬手亮屏状态设置', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 写入疗程
export const setLaserTreatmentParameters = (treatment, remote, actionCallBack) => {
    return new Promise((resolve, reject) => {
        let length = treatment.parameters.length * 4 + 8;
        let dataBuffer = new ArrayBuffer(length);
        let dataView = new DataView(dataBuffer);
        let parameterArray = treatment.parameters;
        dataView.setUint8(0, treatment.index);
        dataView.setUint8(1, parameterArray.length);
        dataView.setUint8(2, treatment.periodic);
        dataView.setUint8(3, treatment.gap);
        if (treatment.endDate != undefined) {
            if (typeof treatment.endDate == 'number') {
                const year = parseInt(treatment.endDate);
                dataView.setUint8(4, year % 256);
                dataView.setUint8(5, year / 256);
                dataView.setUint8(6, 0);
                dataView.setUint8(7, 0);
            } else {
                const year = parseInt(treatment.endDate[0]);
                dataView.setUint8(4, year % 256);
                dataView.setUint8(5, year / 256);
                dataView.setUint8(6, parseInt(treatment.endDate[1]));
                dataView.setUint8(7, parseInt(treatment.endDate[2]));
            }
        } else {
            return
        }
        parameterArray.forEach((parameter, index) => {
            dataView.setUint8(8 + index * 4 + 0, parameter.power);
            dataView.setUint8(8 + index * 4 + 1, parameter.duration);
            dataView.setUint8(8 + index * 4 + 2, parameter.startHour);
            dataView.setUint8(8 + index * 4 + 3, parameter.startMinute);
        })
        BluetoothManager.write(cmd.kGXYL_LaserRegimenParameters, dataView, '写入疗程', getDeviceId(remote), remote)
            .then(res => {
                actionCallBack && actionCallBack()
                console.log("设置疗程参数成功", res)
            })
            .catch((err) => {
                console.log("设置疗程参数失败", res)
                reject(err);
            });
    });
};

// 设置疗程间隙
export const LaserTreatmentInterval = (workDays, remote, status = 1) => {
    return new Promise((resolve, reject) => {
        let state = status ? status : 1
        const timeBuffer = new ArrayBuffer(2);
        let dataView = new DataView(timeBuffer);
        dataView.setUint8(0, state);
        dataView.setUint8(1, workDays);
        BluetoothManager.write(cmd.kGXYL_SetlaserTreatmentStatus, dataView, '设置疗程间隙', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}


// 暂停/开启疗程
export const setLaserTimeOut = (isOpen, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_setLaserRegimen, createBuffer([isOpen]), '暂停/开启疗程', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}

// 查询表盘主题
export const getBleTheme = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GetQuerysubject, createBuffer([]), '查询表盘主题', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}

// 设置表盘主题
export const setBleTheme = (index, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_SetQuerysubject, createBuffer([index]), '设置表盘主题', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}

// 查询血压校准参数
export const getBleBloodParams = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GetCalibration, createBuffer([]), '查询血压校准参数', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}

// 查询血压校准参数
export const setBleBloodParams = (systolic, diastolic, remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_SetCalibration, createBuffer([systolic, diastolic]), '查询血压校准参数', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}


// 血压提醒测量时间查询
export const getBleAutomaticTime = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GetAutomatictime, createBuffer([]), '血压提醒测量时间查询', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}

// 设置血压提醒测量时间
export const setBleAutomaticTime = (arrData, remote) => {
    return new Promise((resolve, reject) => {
        const buffer = new ArrayBuffer(9);
        const dataView = new DataView(buffer);
        for (let i = 0; i < 9; i += 1) {
            dataView.setUint8(i, arrData[i]);
        }
        BluetoothManager.write(cmd.kGXYL_SetAutomatictime, dataView, '设置血压提醒测量时间', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}

// 血压睡眠模式参数查询
export const getBleBloodSleepParams = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GetSleep, createBuffer([]), '血压睡眠模式参数查询', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}


// 设置血压睡眠模式参数查询
export const setBleBloodSleepParams = (arrData, remote) => {
    return new Promise((resolve, reject) => {
        const buffer = new ArrayBuffer(6);
        const dataView = new DataView(buffer);
        for (let i = 0; i < 6; i += 1) {
            dataView.setUint8(i, arrData[i]);
        }
        BluetoothManager.write(cmd.kGXYL_SetSleep, dataView, '血压睡眠模式参数查询', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}




// 设置佩戴位置标签
export const setPositionTag = (index) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_SETPOSTIONTAG, createBuffer([index]), '设置佩戴位置标签', getDeviceId())
            .then(res => {
                resolve(res)
            })
            .catch((err) => {
                reject(err);
            });
    });
}

// 查询佩戴位置标签
export const getPositionTag = () => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GETPOSTIONTAG, createBuffer([]), '查询佩戴位置标签', getDeviceId())
            .then(res => {
                resolve(res)
            })
            .catch((err) => {
                reject(err);
            });
    });
}

// 判断夏令时
function isDaylightTime() {
    var currentDate = new Date();
    var summerTimeStr = currentDate.toLocaleString('en', {
        timeZoneName: 'short'
    }).split(' ')[2];
    return summerTimeStr === "EDT" || summerTimeStr === "CDT" || summerTimeStr === "MDT" || summerTimeStr === "PDT";
}

// 计算时间戳，考虑夏令时
function calculateTimestamp() {
    var timestamp2 = new Date("2017-01-01T00:00:00").getTime() / 1000;
    var currentTimestamp = new Date().getTime() / 1000;
    if (isNaN(timestamp2) || isNaN(currentTimestamp)) {
        console.error('Invalid date in timestamp calculation.');
        return NaN;
    }
    var timezoneOffset = isDaylightTime() ? 3600 : 0; // 夏令时偏移
    return parseInt(currentTimestamp - timestamp2 + timezoneOffset);
}
// 同步设备时间
export const syncTime = () => {
    return new Promise((resolve, reject) => {
        var timestamp = calculateTimestamp(); // 调用新的函数计算时间戳
        var timeBuffer = new ArrayBuffer(4);
        var timeDataView = new DataView(timeBuffer);
        timeDataView.setUint8(0, timestamp & 0x000000FF);
        timeDataView.setUint8(1, (timestamp & 0x0000FF00) >> 8);
        timeDataView.setUint8(2, (timestamp & 0x00FF0000) >> 16);
        timeDataView.setUint8(3, (timestamp & 0xFF000000) >> 24);
        BluetoothManager.write(cmd.kGXYL_TimeSync, timeDataView, '发送时间同步指令', getDeviceId())
            .then(() => {
                resolve();
            })
            .catch((error) => {
                console.log(error, '同步时间的参数error');
                reject(error);
            });
    });
}

// 设置离线疗程
export const setLaserTreatmentParameterOnline = (len, index, treatment) => {
    if (!treatment || !treatment.parameter_list || !treatment.parameter_list.length) {
        return null;
    }
    const length = treatment.parameter_list.length * 4 + 10;
    const dataBuffer = new ArrayBuffer(length);
    const dataView = new DataView(dataBuffer);
    const parameterArray = treatment.parameter_list;
    dataView.setUint8(0, len);
    dataView.setUint8(1, index);
    dataView.setUint8(2, treatment.course_type_sn);
    dataView.setUint8(3, parameterArray.length);
    dataView.setUint8(4, treatment.course_cycle_work_days);
    dataView.setUint8(5, treatment.course_cycle_rest_days);
    if (treatment.course_days_num !== undefined) {
        if (typeof treatment.course_days_num === 'number') {
            const year = parseInt(treatment.course_days_num);
            dataView.setUint8(6, year % 256);
            dataView.setUint8(7, Math.floor(year / 256));
            dataView.setUint8(8, 0);
            dataView.setUint8(9, 0);
        } else {
            const year = parseInt(treatment.course_days_num[0]);
            dataView.setUint8(6, year % 256);
            dataView.setUint8(7, Math.floor(year / 256));
            dataView.setUint8(8, parseInt(treatment.course_days_num[1]));
            dataView.setUint8(9, parseInt(treatment.course_days_num[2]));
        }
    } else {
        return null;
    }
    parameterArray.forEach((parameter, index) => {
        dataView.setUint8(10 + index * 4 + 0, parameter.power_level);
        dataView.setUint8(10 + index * 4 + 1, parameter.start_duration);
        dataView.setUint8(10 + index * 4 + 2, parameter.start_time.split(":")[0]);
        dataView.setUint8(10 + index * 4 + 3, parameter.start_time.split(":")[1]);
    });
    BluetoothManager.write(cmd.kGXYL_SETOFFLINECORESE, dataView, '写入离线疗程', getDeviceId())
        .then(res => {
            console.log("写入离线疗程成功", res)
        })
        .catch((err) => {
            console.log("写入离线疗程失败", err)
            reject(err);
        });
};


// 查询离线疗程参数
export const getLaserTreatmentParameterOnline = () => {
    BluetoothManager.write(cmd.kGXYL_GETOFFLINECORESEPARAMS, createBuffer([]), '查询离线疗程参数', getDeviceId())
        .then(res => {
            console.log("查询离线疗程参数成功", res)
        })
        .catch((err) => {
            console.log("查询离线疗程参数失败", err)
            reject(err);
        });
};


// 获取设备信息
export const getBleDeviceInfo = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GetDeviceInfo, createBuffer([]), '获取设备信息', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 获取睡眠模式状态
export const getBleShock = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GET_AUTO_LASER_SHOCK, createBuffer([]), '获取睡眠模式状态', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 获取睡眠模式状态
export const setBleShock = (remote, isOpen) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_AUTO_LASER_SHOCK, createBuffer([isOpen]), '设置睡眠模式状态', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 查询新法规血氧心率开关
export const getHideBloodHeartStatus = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GETHEARTBLOOD, createBuffer([]), '查询新法规血氧心率开关', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 设置新法规血氧心率开关
export const setHideBloodHeartStatus = (remote, isOpen) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_SETHEARTBLOOD, createBuffer([isOpen]), '设置新法规血氧心率开关', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 开始查询智能激光输出开关状态
export const getHeartLaserStatus = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GETHEARTLASER, createBuffer([]), '查询智能激光输出开关状态', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 开始设置智能激光输出开关状态
export const setHeartLaserStatus = (remote, isOpen) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_SETHEARTLASER, createBuffer([isOpen]), '设置智能激光输出开关状态', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 获取低电阀值
export const getLowerEqThreshold = (remote,) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_getLowerEq, createBuffer([]), '获取低电阀值', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};


// 设置低电阀值
export const setLowerEqThreshold = (start, end, remote,) => {
    return new Promise((resolve, reject) => {
        var timeBuffer = new ArrayBuffer(4)
        var dataView = new DataView(timeBuffer);
        dataView.setUint8(0, start & 0x000000FF);
        dataView.setUint8(1, (start & 0x0000FF00) >> 8);
        dataView.setUint8(2, end & 0x000000FF);
        dataView.setUint8(3, (end & 0x0000FF00) >> 8);
        BluetoothManager.write(cmd.kGXYL_setLowerEq, dataView, '设置低电阀值', getDeviceId(remote), remote)
            .then(res => {
                console.log("设置低电阀值", res)
            })
            .catch((err) => {
                console.log("设置低电阀值", res)
                reject(err);
            });
    });
};


// 指针调节
export const setHourNewPointer = (dic, remote) => {
    return new Promise((resolve, reject) => {
        try {
            const { status, type } = dic;
            const isHour = type === 'hour';
            // 情况：状态为 0 或 7，使用单字节指令
            if (status === 0 || status === 7) {
                const buffer = new ArrayBuffer(1);
                const view = new DataView(buffer);
                view.setInt8(0, status === 0 ? 1 : 0);
                BluetoothManager.write(cmd.kGXYL_set12NewPointer, view, '指针调节', getDeviceId(remote), remote)
                    .then(res => {
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });

                return;
            }
            const statusMap = {
                1: [1, 1],
                2: [0, 1],
                3: [1, 0],
                4: [0, 0],
                5: [1, 2],
                6: [0, 2]
            };
            if (!(status in statusMap)) {
                return reject(new Error(`Unsupported status: ${status}`));
            }
            const [b2, b3] = statusMap[status];
            const buffer = new ArrayBuffer(4);
            const view = new DataView(buffer);
            view.setInt8(0, 2);
            view.setUint8(1, isHour ? 0 : 1);
            view.setInt8(2, b2);
            view.setInt8(3, b3);
            BluetoothManager.write(cmd.kGXYL_set12NewPointer, view, '指针调节', getDeviceId(remote), remote)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                });

        } catch (error) {
            reject(error);
        }
    });
};

// HA05T机芯设置指针调整
export const setBleTime = (times, remote) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(3);
        var dataView = new DataView(buffer);
        if (times.type + '' == "1" && times.hour + '' != "null" && times.minute + '' != "null") {
            dataView.setInt8(0, parseInt(times.type));
            dataView.setUint8(1, parseInt(times.hour));
            dataView.setInt8(2, parseInt(times.minute));
        } else if (times.type + '' == "0" && times.sec + '' != "null") {
            dataView.setInt8(0, parseInt(times.type));
            dataView.setUint8(1, parseInt(times.value % 256));
            dataView.setInt8(2, parseInt(times.value / 256));
        }
        BluetoothManager.write(cmd.kGXYL_setPointer, dataView, 'HA05T设置指针调整', getDeviceId(remote), remote)
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};

// 设置自动心率监测开关
export const getBleHeartAutoState = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GetAutoHRState, createBuffer([]), '查询自动心率监测开关', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 设置自动心率监测开关
export const setBleHeartAutoState = (remote, isOpen) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_HRAutomaticallIsOpen, createBuffer([isOpen]), '设置自动心率监测开关', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
}

// 查询睡眠时间
export const getBleSleepDate = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GETSLEEPDATE, createBuffer([]), '查询睡眠时间', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 设置睡眠时间--起床时间
export const setBleSleepDate = (remote, params) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(4);
        var dataView = new DataView(buffer);
        dataView.setInt8(0, parseInt(params[0]));
        dataView.setUint8(1, parseInt(params[1]));
        dataView.setInt8(2, parseInt(params[2]));
        dataView.setInt8(3, parseInt(params[3]));
        BluetoothManager.write(cmd.kGXYL_SETSLEEPDATE, dataView, '设置睡眠时间', getDeviceId(remote), remote)
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });

    });
};

// 查询步数数据
export const getBleSportData = (params) => {
    const { year, month, day, } = params
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(4);
        var dataView = new DataView(buffer);
        dataView.setUint8(0, parseInt(year % 256));
        dataView.setUint8(1, parseInt(year / 256));
        dataView.setUint8(2, parseInt(month));
        dataView.setUint8(3, parseInt(day));
        BluetoothManager.write(cmd.kGXYL_GetMotionRecording, dataView, '查询步数', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                console.log("发送失败", err)
                reject(err);
            });

    });
};

// 查询激光数据
export const getBleLaserData = (params) => {
    const { year, month, day, startIndex, length } = params
    return new Promise((resolve, reject) => {
        var startIndex1 = startIndex || 0;
        var length1 = length || 100;
        var buffer = new ArrayBuffer(6);
        var dataView = new DataView(buffer);
        dataView.setUint8(0, parseInt(year % 256));
        dataView.setUint8(1, parseInt(year / 256));
        dataView.setUint8(2, parseInt(month));
        dataView.setUint8(3, parseInt(day));
        dataView.setUint8(4, parseInt(startIndex1));
        dataView.setUint8(5, parseInt(length1));
        BluetoothManager.write(cmd.kGXYL_GetLaserRecording, dataView, '查询激光数据', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });

    });
};


// 查询心率数据
export const getBleHeartData = (params) => {
    const { year, month, day, startIndex, length } = params
    return new Promise((resolve, reject) => {
        var startIndex1 = startIndex || 0;
        var length1 = length || 100;
        console.log(parseInt(year % 256))
        console.log(parseInt(year / 256))
        console.log(parseInt(month))
        console.log(parseInt(day))
        console.log(startIndex1)
        console.log(length1)
        var buffer = new ArrayBuffer(6);
        var dataView = new DataView(buffer);
        dataView.setUint8(0, parseInt(year % 256));
        dataView.setUint8(1, parseInt(year / 256));
        dataView.setUint8(2, parseInt(month));
        dataView.setUint8(3, parseInt(day));
        dataView.setUint8(4, parseInt(startIndex1));
        dataView.setUint8(5, parseInt(length1));
        BluetoothManager.write(cmd.kGXYL_GetHRRecording, dataView, '查询心率数据', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });

    });
};

// 查询血压数据
export const getBleBloodData = (params) => {
    const { year, month, day, startIndex, length } = params
    return new Promise((resolve, reject) => {
        var startIndex1 = startIndex || 0;
        var length1 = length || 100;
        var buffer = new ArrayBuffer(6);
        var dataView = new DataView(buffer);
        dataView.setUint8(0, parseInt(year % 256));
        dataView.setUint8(1, parseInt(year / 256));
        dataView.setUint8(2, parseInt(month));
        dataView.setUint8(3, parseInt(day));
        dataView.setUint8(4, parseInt(startIndex1));
        dataView.setUint8(5, parseInt(length1));
        BluetoothManager.write(cmd.kGXYL_GetHistoricalBloodPressurevalue, dataView, '查询血压数据', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });

    });
};



// 查询睡眠数据
export const getBleSleepData = (params) => {
    const { year, month, day, startIndex = 0, limit = 100 } = params
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(6);
        var dataView = new DataView(buffer);
        dataView.setInt8(0, year & 0xFF);
        dataView.setUint8(1, (year >> 8) & 0xFF);
        dataView.setInt8(2, month);
        dataView.setInt8(3, day);
        dataView.setInt8(4, startIndex);
        dataView.setInt8(5, limit);
        BluetoothManager.write(cmd.kGXYL_GETSLEEPDATA, dataView, '查询睡眠数据', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                console.log("发送失败", err)
                reject(err);
            });

    });
};

// 查询闹钟
export const getBleColock = (remote) => {
    return new Promise((resolve, reject) => {
        BluetoothManager.write(cmd.kGXYL_GETCOLOCK, createBuffer([]), '查询闹钟设置时间', getDeviceId(remote), remote)
            .then(resolve)
            .catch((err) => {
                reject(err);
            });
    });
};

// 设置闹钟
export const setBleColock = (remote, data) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(12);
        var dataView = new DataView(buffer);
        for (let i = 0; i < 12; i += 1) {
            dataView.setUint8(i, data[i]);
        }
        BluetoothManager.write(cmd.kGXYL_SETCOLOCK, dataView, '设置闹钟时间', getDeviceId(remote), remote)
            .then(res => {
            })
            .catch((err) => {
                console.log("发送失败", err)
                reject(err);
            });
    });
};

// 设置E币
export const setEbiValue = (ebi) => {
    return new Promise((resolve, reject) => {
        var _minute = parseInt(parseFloat(ebi).toFixed(2) * 100);
        if (_minute == 0) {
            _minute = 1;
        }
        var timeBuffer = new ArrayBuffer(4);
        var dataView = new DataView(timeBuffer);
        dataView.setUint8(0, _minute & 0x000000FF);
        dataView.setUint8(1, (_minute & 0x0000FF00) >> 8);
        dataView.setUint8(2, (_minute & 0x00FF0000) >> 16);
        dataView.setUint8(3, (_minute & 0xFF0000FF) >> 24);
        BluetoothManager.write(cmd.kGXYL_LaserManuallyPayParameters, dataView, '设置E币', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });

    });
};

// 恢复出厂
export const recoverBleDevice = () => {
    return new Promise((resolve, reject) => {
        var timeBuffer = new ArrayBuffer(0);
        var dataView = new DataView(timeBuffer);
        BluetoothManager.write(cmd.kGXYL_RestoreFactorySettings, dataView, '恢复出厂', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};

// 查询设备wifi
export const getWifiStatus = (data) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(1);
        var dataView = new DataView(buffer);
        BluetoothManager.write(cmd.kGXYL_GETWIFIStatus, dataView, '获取设备WIFI开关', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};
// 开启设备wifi
export const setWifiStatus = (data) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(1);
        var dataView = new DataView(buffer);
        dataView.setUint8(0, data);
        BluetoothManager.write(cmd.kGXYL_SETTWIFIStatus, dataView, '开启设备wifi', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};

// 查询获取WIFI实时上传开关
export const getRealTimeWifi = (remote) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(1);
        var dataView = new DataView(buffer);
        BluetoothManager.write(cmd.kGXYL_GetRealTime_Wifi, dataView, '获取WIFI实时上传开关', getDeviceId(remote), remote)
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });

    });
};

// 设置获取WIFI上传开关
export const setRealTimeWifi = (remote, status) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(1);
        var dataView = new DataView(buffer);
        dataView.setUint8(0, status);
        BluetoothManager.write(cmd.kGXYL_SetRealTime_Wifi, dataView, '设置WIFI实时上传开关', getDeviceId(remote), remote)
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};

// 获取WIFI定时上传开关
export const getTimeUpload = (remote, status) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(13);
        var dataView = new DataView(buffer);
        BluetoothManager.write(cmd.kGXYL_GetTimeUpload_Wifi, dataView, '获取WIFI实时上传开关', getDeviceId(remote), remote)
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};

// 设置WIFI定时上传开关
export const setTimeUpload = (remote, data) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(13);
        var dataView = new DataView(buffer);
        for (let i = 0; i < 13; i += 1) {
            dataView.setUint8(i, data[i]);
        }
        BluetoothManager.write(cmd.kGXYL_SetTimeUpload_Wifi, dataView, '设置WIFI定时上传开关', getDeviceId(remote), remote)
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};

// 查询硬件当前的使用的疗程
export const getBleCurrentCourse = (data) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(1);
        var dataView = new DataView(buffer);
        BluetoothManager.write(cmd.kGXYL_GetLaserRegimenParameters, dataView, '查询硬件当前的使用的疗程', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};

// 切换硬件的离线疗程
export const switchBleOfflineCourse = (course_sn) => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(1);
        var dataView = new DataView(buffer);
        dataView.setUint8(0, course_sn);
        BluetoothManager.write(cmd.kGXYL_SWITCHOFFLINECOURSESN, dataView, '切换硬件的离线疗程', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};

// 获取激光疗程状态
export const getBleCourseOffline = () => {
    return new Promise((resolve, reject) => {
        var buffer = new ArrayBuffer(1);
        var dataView = new DataView(buffer);
        BluetoothManager.write(cmd.kGXYL_GetlaserTreatmentStatus, dataView, '获取硬件的疗程状态', getDeviceId())
            .then(res => {
            })
            .catch((err) => {
                reject(err);
            });
    });
};